package com.otitan.la.forest.industry.utils;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.otitan.auth.framework.basepro.common.AuthCommon;
import com.otitan.la.forest.industry.constant.StaticConstant;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Description:    公共工具类
 * @Author:        Sunke
 * @CreateDate:     2020/10/16 0016 11:33
 * @UpdateUser:     Sunke
 * @UpdateDate:     2020/10/16 0016 11:33
 * @UpdateRemark:   修改内容
 * @Version:        1.0
 */
public class CommonUtils {


    /**
     * 生成token
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 更具字典值获取字典名称
     *
     * @param []
     * @return java.lang.String
     * @throws
     * @author jiachangsen
     * @date 2020/10/16 17:38
     */
    public static String getDicName(StringRedisTemplate redisTemplate,
                                    String redisKeyPrefix,
                                    String typeKey,
                                    String key) {
        if (key == null) {
            return null;
        }
        Object dicName = redisTemplate.opsForHash().get(redisKeyPrefix + StaticConstant.DICTIONARY + typeKey, key);
        return dicName == null ? null : dicName.toString();

    }

    /**
    * 获取字典Map
    * @author Sunke
    * @param [typeKey, redisTemplate, redisKeyPrefix]
    * @return java.util.Map
    * @exception
    * @date 2020/10/21 0021 18:25
    */
    public static Map getDicName(String typeKey, RedisTemplate redisTemplate, String redisKeyPrefix) {
        if (typeKey.equals("AREA")) {
            Map areaMap = redisTemplate.opsForHash().entries(redisKeyPrefix + typeKey + ":");
            return areaMap == null ? null : areaMap;
        }
        Map map = redisTemplate.opsForHash().entries(redisKeyPrefix + StaticConstant.DICTIONARY + typeKey);
        return map == null ? null : map;
    }



    /**
     * 从List<A> copy到List<B>
     *
     * @param list
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> copy(List<?> list, Class<T> clazz) {
        String oldOb = JSON.toJSONString(list);
        return JSON.parseArray(oldOb, clazz);
    }

    /**
     * 从对象A copy到 对象B
     *
     * @param ob    A
     * @param clazz B.class
     * @return B
     */
    public static <T> T copy(Object ob, Class<T> clazz) {
        String oldOb = JSON.toJSONString(ob);
        return JSON.parseObject(oldOb, clazz);
    }

    /**
     * 判断集合是否为空
     *
     * @return B
     */
    public static boolean isEmpty(List list) {
        boolean isEmpty=true;
        if(list!=null &&  list.size()>0){
            isEmpty=false;
        }
        return isEmpty;
    }

    /**
     * 获取组织机构信息
     * @author jiachangsen
     * @param []
     * @return com.alibaba.fastjson.JSONObject
     * @exception
     * @date 2020/10/21 16:38
     */
    public static JSONObject getUserOrgInfo(AuthCommon authCommon){
        JSONArray orgInfo = authCommon.getLoginOrgInfo();
        if (CollUtil.isNotEmpty(orgInfo)){
            return JSONObject.parseObject(orgInfo.get(0).toString());
        }
        return null;
    }
}
